package lv.rtu.ditf.dc.classification;

import java.util.HashMap;

/**
 * 
 * @author Pāvels Koliškins, Edgars Matisāns
 *
 */
public class ClassificationUtils {

    // method returns Cosine similarity between two documents
    public static double getCosineSimilarity(HashMap<String, Integer> documentToClassify,
            HashMap<String, Double> documentInCategory) {

        //TODO POSSIBLE: add document NORM as attribute to Document`s class?

        // calculate norm of documentToClassify
        double documentToClassifyNorm = 0.0;
        for (Integer weight: documentToClassify.values()) {
            documentToClassifyNorm += (weight * weight);
        }
        documentToClassifyNorm = Math.sqrt(documentToClassifyNorm);
        
        // calculate norm of documentInCategory
        double documentInCategoryNorm = 0.0;
        for (Double weight: documentInCategory.values()) {
            documentInCategoryNorm += (weight * weight);
        }
        documentInCategoryNorm = Math.sqrt(documentInCategoryNorm);

        // iterate through documentToClassify vector and find similar words in documentInCategory
        double sumOfSimmilarWords = 0.0;
        for (String word: documentToClassify.keySet()) {
            if (documentInCategory.get(word) != null) {
                // similarity between two vectors were found
                sumOfSimmilarWords += documentInCategory.get(word) * documentToClassify.get(word);
            }
        }

        return sumOfSimmilarWords / (documentToClassifyNorm * documentInCategoryNorm);
    }
}
